package com.love.cloud.admin.notify.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.cloud.admin.notify.entity.NotifyReply;
import com.love.cloud.admin.notify.mapper.NotifyArticleMapper;
import com.love.cloud.admin.notify.mapper.NotifyReplyMapper;
import com.love.cloud.admin.notify.service.NotifyReplyService;
import com.love.cloud.admin.notify.vo.ReplyVo;
import com.love.cloud.common.core.config.SnowflakeConfig;
import com.love.cloud.common.mybatis.util.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author hongdongqu
 */
@Service
public class NotifyReplyServiceImpl extends ServiceImpl<NotifyReplyMapper, NotifyReply> implements NotifyReplyService {
    @Resource
    private NotifyArticleMapper notifyArticleMapper;
    @Resource
    private PageUtils pageUtils;
    @Resource
    private SnowflakeConfig snowflakeConfig;

    @Override
    public Object getReplyList(Long articleId, ReplyVo replyVo) {
        Page page = pageUtils.page();
        return this.baseMapper.replyPageInfo(page,articleId,replyVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveArticleReply(NotifyReply notifyReply) {
        if(notifyReply.getStoreyNum()!=null) {
            NotifyReply updateReply=new NotifyReply();
            updateReply.setArticleId(notifyReply.getArticleId());
            updateReply.setStoreyNum(notifyReply.getStoreyNum());
            updateReply.setChildStoreyNum(0);
            //修改回复评论数
            this.baseMapper.updateReplyNum(updateReply);
        }
        //修改文章评论回复数量
        notifyArticleMapper.updateCommentNum(notifyReply.getArticleId());
        notifyReply.setId(snowflakeConfig.snowflakeId());
        notifyReply.setCreateTime(LocalDateTime.now());
        notifyReply.setUpdateTime(LocalDateTime.now());
        return this.baseMapper.saveReply(notifyReply);
    }
}
